package atom.core0.j2ee.page;
import java.io.Serializable;
import java.util.List;
 
/**
 * 分页封装类
 * 用于做分页查询的基础类，封装了一些分页的相关属性
 * @author hike_man
 * @version v1.0
 * @param <T>
 */
public class PageResult implements Serializable{
 
    // 下一页
    private int nextPageNo;
 
    // 当前页
    private int currentPage=1;
 
    // 每页个个数
    private int pageSize;
 
    // 总条数
    private int totalRowCount;
 
    // 总页数
    private int totalPageCount;
 
    private int index;
	
    // 记录
    private List data;
    public PageResult()
    {
    	
    }
	public PageResult(int totalRowCount, int pageSize,int currentPage) {
		this.totalRowCount = totalRowCount;
		this.pageSize = pageSize;
		this.countMaxPage();
		this.setCurrentPage(currentPage);
		
	}
	private void countMaxPage() {
		if (this.totalRowCount % this.pageSize == 0) {
			this.totalPageCount = this.totalRowCount / this.pageSize;
		} 
		else {
			this.totalPageCount = this.totalRowCount / this.pageSize + 1;
		}
	}

    public int getTotalRowCount() {
		return totalRowCount;
	}

	public void setTotalRowCount(int totalRowCount) {
		this.totalRowCount = totalRowCount;
	}

	public int getTotalPageCount() {
		return totalPageCount;
	}

	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}

	public int getNextPageNo() {
        if (nextPageNo <= 0) {
            return 1;
        } else{
            return nextPageNo > totalPageCount ? totalPageCount : nextPageNo;
        }
    }
 
    public void setNextPageNo(int nextPageNo) {
        this.nextPageNo = nextPageNo;
    }
 

 
    public List getData() {
		return data;
	}
	public void setData(List data) {
		this.data = data;
	}
	public int getCurrentPage() {
        return currentPage;
    }
 
	//去掉了大于最后一页 是 最后一页的特性
	public void setCurrentPage(int currentPage) {
		if (currentPage < 1) {
			this.currentPage = 1;
		} 
//		else if (currentPage > this.totalPageCount) {
//			this.currentPage = this.totalPageCount;
//		} 
		else {
			this.currentPage = currentPage;
		}

//		if (this.currentPage < 1) {
//			this.currentPage = 1;
//		}
	}
 
    public int getPageSize() {
        return pageSize;
    }
 
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize <= 0 ? 10 : pageSize;
    }

 
//    public void resetNextPageNo() {
//    	nextPageNo = currentPage + 1;
//        totalPageCount = totalRowCount % pageSize == 0 ? totalRowCount / pageSize
//                : totalRowCount / pageSize + 1;
//    }
    
	public int getFirstPage() {
		return 1;
	}

	public int getPriorPage() {
		if (this.currentPage == 1) {
			return this.currentPage;
		}

		return (this.currentPage - 1);
	}

	public int getLastPage() {
		return this.totalPageCount;
	}

	public int getNextPage() {
		if (this.currentPage == this.totalPageCount) {
			return this.currentPage;
		}

		return (this.currentPage + 1);
	}
	public List getBatchPages(int batchSize) {
		return new BatchPage(this, batchSize).getBatchPages();
	}
	public int getOffsetIndex() {
		if (currentPage>0)
			return (currentPage - 1) * pageSize;
		else
			return 0;
	}
}
